;(function(w,$,doc) {
    var defaults = {
        // 进行排序的单元格序号
        index: [],
        // 进行排序的单元格表头父元素
        thead: '',
        tbody: 'tbody',
        // 默认排序方式，asc和desc，不设置，点击后默认为desc,数组，index对应设置排序的列
        way: []
    };
    $.fn.sort = function (option) {
        var options = this.options = $.extend({},defaults,option),
            oThead = $(options.thead),
            aTd = oThead.find("td");
        var aEle = [],
            index = options.index;
        $.each(index,function(index,item) {
           aEle.push({'index': item, 'obj': aTd[item]});
        });
        this.aEle = aEle;
        var that = this;
        var sN = $.fn.sort.prototype,
            tbody = options.tbody;
        $.each(this.aEle,function (index,value) {
            var obj = that.aEle[index].obj,
                oIndex = that.aEle[index].index,
                arrWay = options.way;
            sN.getWay.apply(obj,[arrWay[index]]);
            $(obj).append('<i class="iconfont icon-asc"></i>' +
                            '<i class="iconfont icon-desc"></i>');
            oThead.parent()
                .off('.sorting')
                .on('click.sorting','td',function() {
                // 当前正排序的为深色
                $(this).siblings('td')
                    .find('.iconfont')
                    .removeClass('sorting');
                $(this).find('.iconfont')
                    .addClass('sorting');
                var way = sN.getWay.apply(this);
                var data = sN.getData.apply(this, [oIndex]);
                var arr = sN.decideWay(way, data);
                sN.reloadTr(arr,tbody);
            });
        });
        return this;
    };
    $.fn.sort.prototype = {
        getWay: function (defaultWay) {
                var way = $(this).attr('data-direction');
                if(!way && defaultWay) {
                    way = defaultWay;
                }
                else {
                    way = way === 'asc'? 'desc':'asc';
                }
                $(this).attr('data-direction',way);
                return way;
            },
        // 获取排序数据
        getData: function (index) {
            var othead = $(this).closest('thead'),
                otbody = othead.next('tbody'),
                aTr = otbody.children('tr'),
                data = [];
            $.each(aTr,function(indx,value) {
                var oTr = $(aTr[indx]),
                    oTd = oTr.find('td:eq('+index+')'),
                    oTdVal = oTd.text();
                data.push(oTdVal);
            });
            return data;
        },
        // 排序
        decideWay: function decideWay(way,data) {
            var res = [],
                isNum = true;
            // 判断是否为数字
            $.each(data,function(index,item) {
                var val = data[index];
                if(val != +val) {
                    isNum = false;
                }
                res.push({'value':val,'id':index});
            });
            // 数字按大小判断
            if(isNum) {
                var rev = way === 'asc';
                res.sort(function (a,b) {
                    a = a.value;
                    b = b.value;
                    if(rev) {
                        return a - b;
                    }
                    else {
                        return b - a;
                    }
                });
            }
            // 非数字按unicode
            else {
                res.sort(function(a,b) {
                    a = a.value;
                    b = b.value;
                    var rev = way === 'asc' ? 1 : -1;
                    return rev * a.toString().localeCompare(b);
                })
            }
            // 判断tr序号
            var arr = [];
            $.each(res,function(index,value) {
                var num = '';
                for(i = 0;l = res.length,i < l;i++) {
                    var id = res[i].id;
                    if(id == index) {
                        num = i;
                    }
                }
                arr.push(num);
            });
            return arr;
        },
        // 渲染数据
        reloadTr: function (arr,tbody) {
            var oTbody = $(tbody),
                aTr = oTbody.children('tr'),
                oHtml = '',
                res = [aTr.length];
            //console.log(arr)
            $.each(arr,function(index,item) {
                res[item] = aTr[index].outerHTML;
            });
            $.each(res,function(index,item) {
                oHtml += res[index];
            });
            oTbody.html(oHtml);
        }
};
    w.sort = $.fn.sort;
})(window,jQuery,document);